package Demo2;

class Money implements Cloneable{
    double money=24;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Student implements Cloneable{
    public String name;
    public int age;
    Money m=new Money();
    public Student(String name,int age){
        this.name=name;
        this.age=age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", money=" + m.money +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Student tmp=(Student)super.clone();//克隆了Student里面的值
        tmp.m= (Money) this.m.clone();//克隆money
        return tmp;
    }
}
public class Test {
    public static void main(String[] args)throws CloneNotSupportedException {
        Student student=new Student("张三",18);
        Student student1=(Student) student.clone();
        System.out.println(student);
        System.out.println(student1);
        student1.m.money=8;
        System.out.println("修改student1的money的值后");
        System.out.println(student);
        System.out.println(student1);
    }
}
